package com.gm.wholesale.util;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.gm.commonlib.manger.ActivityMangers;
import com.gm.commonlib.network.basenetwork.BaseHttpClient;
import com.gm.commonlib.util.permission.ui.PermissionActivity;
import com.gm.wholesale.bluetooth.BluetoothUtil;
import com.gm.wholesale.bluetooth.DeviceManger;
import com.gm.wholesale.cacheData.CacheData;
import com.gm.wholesale.module.config.ConfigData;
import com.gm.wholesale.module.login.ui.LoginActivity;
import com.gm.wholesale.module.main.ui.MainActivity;
import com.gm.wholesale.module.user.UserDataManger;
import com.gm.wholesale.module.voice.VoiceManger;
import com.gm.wholesale.module.voice.voiceCreateOrder.VoiceCreateOrderManger;

import java.lang.reflect.Method;
import java.util.UUID;

public class AppUtil {
    public static void exitApp(){
       stop();
    }

    public static void loginOut(){
        UserDataManger.newInstance().clear();
        stop();
    }

    public static void stop(){
        BaseHttpClient.getHttpclient().reset();
        ActivityMangers.getActivityMangers().cleanAllActivity();
        if (MainActivity.mActivity!=null){
            MainActivity.mActivity.finish();
            MainActivity.mActivity=null;
        }
        VoiceManger.destory();
        DeviceManger.onDestroy(null);
        VoiceCreateOrderManger.newInstance().stop();
        if (PermissionActivity.activity!=null){
            PermissionActivity.activity.finish();
        }
        ConfigData.getConfigData().clean();
        CacheData.clear();
        BluetoothUtil.newInstance().destroy();
        System.gc();
    }

    public static void startLoginAct(Context context){
        Intent intent=new Intent();
        intent.setClass(context, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        loginOut();
        context.startActivity(intent);
    }

    public  static String getUUid(){
        UUID uuid = UUID.randomUUID();
        String uuids = uuid.toString();
        return uuids;
    }

    public static void hideSoftKeyboard(Context context,View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

    }

    /**
     * 获取版本号
     *
     * @return
     */
    public static int getVersionCode(Context context) {
        return getVersionCode(context, 0);
    }

    public static int getVersionCode(Context context, int defValue) {

            PackageManager manager = context.getPackageManager();//获取包管理器
            try {
                //通过当前的包名获取包的信息
                PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);//获取包对象信息
                return info.versionCode;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return defValue;

    }

    public static String getVersionName(Context context) {

            PackageManager manager = context.getPackageManager();
            try {
                //第二个参数代表额外的信息，例如获取当前应用中的所有的Activity
                PackageInfo packageInfo = manager.getPackageInfo(context.getPackageName(), 0);
                return packageInfo.versionName;
            } catch (PackageManager.NameNotFoundException e) {
                return "";

        }
    }


    public static synchronized String getAppName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            return context.getResources().getString(labelRes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    public static void removeBondDevice(BluetoothDevice device) {
        try {
            Method m = device.getClass()
                    .getMethod("removeBond", (Class[]) null);
            m.invoke(device, (Object[]) null);
        } catch (Exception e) {

        }
    }



}
